package com.pengtu.gsj.service.impl.check.lwfw;

import java.util.ArrayList;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.lwfw.TrafficserviceDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lwfw.Trafficservice;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.lwfw.PubInfoServerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class PubInfoServerServiceImpl implements PubInfoServerService{

	@Autowired
	public SystemManageService systemManageService;
	@Autowired
	public CheckTableService checkTableService;
	@Autowired
	public TrafficserviceDao trafficserviceDao;
	
	@Override
	public Map<String, Object> getPubInfoServers(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Attachment> resultAtts = new ArrayList<Attachment>();
		List<Trafficservice> trafficservices = new ArrayList<Trafficservice>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double tablePart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double tableFull = MathsUtils.sumDouble(yearFull, tablePart);	
		double percentage = 0; //完善率
		double temp = 0; //临时算分变量
		double total = 0; //计算总分
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getCheckAttachments(startTime, endTime, tag);
		//根据是否有信息报送附件判断是否得分
		resultAtts = systemManageService.getAttachmentBy("tag", "gzcx_bszd");
		temp = resultAtts.size() > 0 ? MathsUtils.formatDouble(yearFull, 1) : 0;
		//如果提供路况信息方式达到6种及上得满分，否则按实际算分，total结果始终大于等于零
		trafficservices = trafficserviceDao.getAll();
		if (trafficservices.size() > 0){
			if (trafficservices.size() >= 6){
				total = tablePart;
			} else {
				total = tablePart - (6 - trafficservices.size()) * tablePart/4;
				total = total > 0 ? MathsUtils.formatDouble(total, 1) : 0;
			}
				//控制导出时是否输出和导出
				map.put("flag", true);
			}
		map.put("scoreTwo", total);
		total = MathsUtils.formatDouble(temp + total, 1);
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//无条件查询，由于是属于附件判空，所以每次都进行更新
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("scoreOne", temp);
		map.put("percent", percentage);
		map.put("manageData", trafficservices);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
}
